﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;

namespace CommonLibrary.WPFConvert
{
    
    public class BoolToVisableConvert : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            Visibility viba = Visibility.Collapsed;
            if (value == null)
                return viba;
            bool result = (bool)value;
            return result ? Visibility.Visible : Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    /// <summary>
    /// 将百分比转化成高度
    /// </summary>
    public class ConverterPercentToHeight : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (int)(double.Parse(value.ToString()) * 0.46);
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NullReferenceException();
        }
    }
    /// <summary>
    /// 将百分比转化成颜色
    /// </summary>
    public class ConverterPercentToColor : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            ////  FillArea.Fill = new Brush();
            //color转为brush：  
            Color r = Color.FromRgb(241, 55, 55);
            Color g = Color.FromRgb(38, 186, 100);
            Color y = Color.FromRgb(249, 118, 34);

            int percent;
            bool bRet = int.TryParse(value.ToString(), out percent);

            if (bRet)
            {
                if (percent <= 40)
                {
                    Brush brColor = new SolidColorBrush(g);
                    return brColor;
                }
                else if (percent <= 70)
                {
                    Brush brColor = new SolidColorBrush(y);
                    return brColor;
                }
                else
                {
                    Brush brColor = new SolidColorBrush(r);
                    return brColor;
                }

            }

            Brush br = new SolidColorBrush(g);
            return br;
            //return (int)(double.Parse(value.ToString()) * 3.6);
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NullReferenceException();
        }
    }
    public class ConverterBoolToCollapsed : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null || !(value is bool))
                return Visibility.Visible;
            return (bool)value ?
                Visibility.Collapsed : Visibility.Visible;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public class BoolNegationConvert : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return !((bool)value);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return !((bool)value);
        }
    }
}
